<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            :render-content="renderContent"
            ref="tree"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户名称" prop="userName">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入用户名称"
              clearable
              style="width: 150px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="primary" round icon="el-icon-user-solid" size="mini"
                       @click="dataTableVisiable=false">已选（{{selectUsers.length}}）</el-button>
          </el-form-item>
        </el-form>
        <el-table v-show="dataTableVisiable==true" ref="dataTable"
                  :data="userList"
                  :row-key="getRowKeys"
                  :reserve-selection="true"
                  @select="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="用户编号" align="center" prop="username"   />
          <el-table-column label="用户名称" align="center" prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="部门" align="center" prop="departname"   :show-overflow-tooltip="true" />
        </el-table>

        <pagination v-if="dataTableVisiable==true"
          v-show="total>0"
          :total="total"
                    :pageSizes="[5,10,20]"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"

          @pagination="getList"
        />

        <el-table v-show="dataTableVisiable==false" :data="selectUsers" style="">
          <el-table-column type="index" width="50" label="序号"></el-table-column>
          <el-table-column prop="name" label="人员名称"></el-table-column>
          <el-table-column prop="username" label="人员帐号" ></el-table-column>
          <el-table-column prop="departname" label="所属部门"></el-table-column>
        </el-table>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import {getDepartmentEntityList} from '@/api/orgm/orgm'
import {getUserInfoList} from '@/api/orgm/user'
import Pagination from '@/components/Pagination';
export default {
  name: "byUsers",
  components: { Pagination },
  props:['selectData','ccData'],
  data(){
    return{
      total:0,
      deptName:'',
      dataTableVisiable:true,
      deptOptions:[],
      selectUsers:this.selectData,
      userList:this.ccData,
      // 显示搜索条件
      showSearch: true,
      defaultProps: {
        children: "children",
        label: "name"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        username: '',
        name: '',
        departid: ''
      },
      searchForm: {
        pageNum: 0,
        pageSize: 0,
        name: '',
        companyid: ''
      },
    }
  },
  mounted() {
    this.getList();
    this.getTreeselect();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    selectData(){
      this.selectUsers=this.selectData;
    }
  },
  methods:{
    /** 查询用户列表 */
    async getList() {
      if(this.userList.length<=0) {
        const res = await getUserInfoList(this.queryParams);
        if (res.error === 200) {
          this.userList = res.result.list;
          this.total = res.result.total;
          this.SelectionChange(this.selectUsers);
        } else {
          this.$message.error(`获取列表失败`);
        }
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      getDepartmentEntityList().then((res) => {
        this.deptOptions = res.result;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      this.dataTableVisiable=true;
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.dataTableVisiable=true;
      this.queryParams.departid = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dataTableVisiable=true;
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    /** 选中/取消选中 **/
    handleSelectionChange(row,unChecked) {
      const rows = this.selectUsers;
      let selected = row.length && row.indexOf(unChecked) !== -1;
      if(selected==false||row.length==0){
        this.selectUsers=rows.filter(r=>{
          if(r.id!=unChecked.id)
            return r;
        })
      }
      else {
        if (this.selectUsers.length > 0) {
          if (row.length > 0) {
            row.forEach(r => {
              let isHas = false;
              rows.forEach(s => {
                if (s.id == r.id) {
                  isHas = true;
                }
              })
              if (isHas == false) {
                this.selectUsers.push(r);
              }
            })
          }
        } else {
          this.selectUsers = row;
        }
      }
    },
    /**默认选中**/
    SelectionChange(rowData) {
      this.$nextTick(() => {
        let table = this.userList;
        this.selectUsers=rowData;
        table.forEach(row => {
          rowData.forEach(selectRow=>{
            if(row.id==selectRow.id){
              this.$refs.dataTable.toggleRowSelection(row, true);
            }
          });
        });
      });
    },
    getRowKeys(row) {
      return row.id
    },
    /** 显示label **/
    renderContent(h,{node}){
      return (
        <span class="custom-tree-node">
          <span title={node.label}>{node.label}</span>
        </span>
      );
    },
    /** 保存方法 **/
    handlerSave(){
      if(this.selectUsers.length<=0){
        this.$message.warning("请选择人员");
        return ;
      }
      return this.selectUsers;
    }
  }
}
</script>

<style scoped>

</style>
